package com.ccsc.auth.cas.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.ccsc.auth.cas.global.Constants;
import org.jasig.cas.authentication.Credential;
import org.jasig.cas.authentication.UsernamePasswordCredential;
import org.jasig.cas.web.flow.AuthenticationViaFormAction;
import org.jasig.cas.web.support.WebUtils;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.util.StringUtils;
import org.springframework.webflow.execution.RequestContext;

/**
 * 自定义的处理验证码登录逻辑的Action
 * @create 2015-10-29 下午4:38:23
 * @author xingwen
 */
public class AuthenticationViaKaptchaFormAction extends AuthenticationViaFormAction {
	public final String validateKaptcha(final RequestContext context, final Credential credential, final MessageContext messageContext) {
        final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
        HttpSession session = request.getSession();

        UsernamePasswordCredential upc = (UsernamePasswordCredential) credential;
        String type = upc.getType();
        String kaptcha = "";

//        switch (type){
//            case "1":
//                kaptcha = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
//                session.removeAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
//                break;
//            case "2":
//                kaptcha = (String) session.getAttribute(Constants.SMS_SESSION_KEY_PREFIX.concat(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY));
//                session.removeAttribute(Constants.SMS_SESSION_KEY_PREFIX.concat(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY));
//                break;
//            default:
//                break;
//        }
//
//
//        String submitAuthcodeKaptcha = upc.getKaptcha();
//
//        if (!StringUtils.hasText(kaptcha) || !StringUtils.hasText(submitAuthcodeKaptcha)) {
//            if("2".equals(type)){
//                messageContext.addMessage(new MessageBuilder().error().code("smskaptcha.required").build());
//            }
//            else{
//                messageContext.addMessage(new MessageBuilder().error().code("kaptcha.required").build());
//            }
//            return "error";
//        }
//        if (submitAuthcodeKaptcha.equals(kaptcha)) {
//            return "success";
//        }
//        if("2".equals(type)){
//            messageContext.addMessage(new MessageBuilder().error().code("error.authentication.smskaptcha.bad").build());
//        }
//        else {
//            messageContext.addMessage(new MessageBuilder().error().code("error.authentication.kaptcha.bad").build());
//        }

        return "success";
    }
}